﻿@using EIP.Common.Entities.Dtos
@using EIP.System.Models.Enums
@using EIP.Web.AdminLTE.DataUsers.Helpers
@{
    ViewBag.Title = "组织机构信息编辑";
    Layout = "~/Views/Shared/_LayoutPageBaseEdit.cshtml";
}
<div class="box-body">
    <div class="col-md-12">
        <div class="form-group">
            <label class="col-sm-2 control-label">上级组织</label>
            <div class="col-sm-10">
                <div class="input-group">
                    <input type="text" name="parentName" id="parentName" value="@Model.ParentName" class="form-control" data-pure-clear-button placeholder="上级模块">
                    <span id="menuBtn" onclick=" showMenu(); return false; " class="input-group-addon pointer"><i class="fa fa-sitemap"></i></span>
                </div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label m">名称</label>
            <div class="col-sm-10">
                <input type="text" name="name" id="name"  value="@Model.Name" data-pure-clear-button class="form-control" placeholder="名称">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label m">简称</label>
            <div class="col-sm-10">
                <input type="text" class="form-control" value="@Model.ShortName" data-pure-clear-button id="ShortName" name="ShortName" placeholder="简称">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label m">代码</label>
            <div class="col-sm-10">
                <input type="text" class="form-control" value="@Model.Code" data-pure-clear-button id="Code" name="Code" placeholder="代码">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label m">公司性质</label>
            <div class="col-sm-10">
                @Html.DropDownListEnum(new DropDownListEnumInput
                {
                    EnumType = (typeof(EnumOrgNature)),
                    Name = "Nature",
                    NeedDefault = true,
                    SelectedVal = @Model.Nature,
                    HtmlAttributes = "class='form-control select2' style='width: 100%;'"
                })
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label m">状态</label>
            <div class="col-sm-10">
                @Html.DropDownListEnum(new DropDownListEnumInput
                {
                    EnumType = (typeof(EnumOrgState)),
                    Name = "State",
                    NeedDefault = true,
                    SelectedVal = @Model.State,
                    HtmlAttributes = "class='form-control select2' style='width: 100%;'"
                })
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label m">主负责人</label>
            <div class="col-sm-10">
                <input type="text" class="form-control" value="@Model.MainSupervisor" data-pure-clear-button id="MainSupervisor" name="MainSupervisor" placeholder="主负责人">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label ">联系方式</label>
            <div class="col-sm-10">
                <input type="text" class="form-control" value="@Model.MainSupervisorContact" data-pure-clear-button id="MainSupervisorContact" name="MainSupervisorContact" placeholder="联系方式">
            </div>
        </div>

        <div class="form-group">
            <label class="col-sm-2 control-label">选项</label>
            <div class="col-sm-10">
                <label>
                    @Html.CheckBoxHtmlString(new CheckBoxInput
                    {
                        Name = "IsFreeze",
                        Title = "冻结",
                        IsCheck = @Model.IsFreeze
                    })
                </label>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label ">序号</label>
            <div class="col-sm-10">
                <input type="text" class="form-control" value="@Model.OrderNo" data-pure-clear-button id="OrderNo" name="OrderNo" placeholder="序号">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label">备注</label>
            <div class="col-sm-10">
                <textarea class="form-control" value="@Model.Remark" data-pure-clear-button id="Remark" name="Remark" placeholder="备注"></textarea>
            </div>
        </div>
    </div>
</div>
@model EIP.System.Models.Dtos.Identity.SystemOrganizationOutput
@Html.Hidden("ParentId", @Model.ParentId)
@Html.Hidden("OrganizationId", @Model.OrganizationId)

<div class="combotree-popup dropdown-menu" id="menuContent" style="display: none;">
    <div class="ip-control">
        <div class="box-tools">
            <div class="input-group" style="width: 272px; padding-left: 5px">
                <input type="text" id="searchOrgKey" class="form-control input-sm" placeholder="请输入名称进行模糊查询...">
                <div class="input-group-btn">
                    <a class="btn btn-sm btn-default" id="searchOrg"><i class="fa fa-search"></i></a>
                </div>
            </div>
        </div>
    </div>
    <ul id="orgTree" class="ztree" style="margin-top: 0; height: 223px; overflow: auto"></ul>
</div>

<script>
    var form = null;
    $(function () {

        //iCheck for checkbox and radio inputs
        $('input[type="checkbox"].minimal, input[type="radio"].minimal').iCheck({
            checkboxClass: 'icheckbox_minimal-blue',
            radioClass: 'iradio_minimal-blue'
        });
        //Red color scheme for iCheck
        $('input[type="checkbox"].minimal-red, input[type="radio"].minimal-red').iCheck({
            checkboxClass: 'icheckbox_minimal-red',
            radioClass: 'iradio_minimal-red'
        });
        //Flat red color scheme for iCheck
        $('input[type="checkbox"].flat-red, input[type="radio"].flat-red').iCheck({
            checkboxClass: 'icheckbox_flat-green',
            radioClass: 'iradio_flat-green'
        });
        $(".icon-picker").iconPicker();
        $.each($("input:text, input:password, " +
                "input[type='email'], input[type='url'], input[type='search'], input[type='tel'], input[type='number'], " +
                "input[type='datetime'],textarea", $("#editform")),
            function (index, element) {
                var $element = $(element);
                if (($element.attr("data-pure-clear-button") == undefined || $element.attr("data-pure-clear-button") === 'false') && !$element.hasClass("has-pure-clear-button")) {
                    return;
                }
                $element.pureClearButton("create");
            });

        ValidformNeed();

        initForm();
    });

    function initForm() {
        //初始化控件
        form = $("#editform").form();
        //数据校验
        $("#editform").formValidation({
            message: '请输入有效值',
            trigger: 'blur',
            fields: {
                name: {
                    validators: {
                        notEmpty: {
                            message: '请输入姓名'
                        }
                    }
                },
                parentName: {
                    validators: {
                        notEmpty: {
                            message: '请选择归属菜单'
                        }
                    }
                }
            }
        }).on('success.form.fv', function (e) {
            var data = form.getFormSimpleData();
            data["ParentId"] = data["parentName"] !== "" ? data["ParentId"] : "";
            UtilAjaxPostWait("/System/Organization/SaveOrganization",
          data, function (data) {
              if (DialogAjaxResult(data)) {
                  orgObj = { pId: $("#ParentId").val() };
                  loadTreeAndGrid();
                  //关闭
                  dialog.close();
              }
          });
            return false;
        });
    }

</script>
<script type="text/javascript">

    var setting = {
        view: {
            dblClickExpand: false
        },
        data: {
            simpleData: {
                enable: true
            }
        },
        callback: {
            onClick: onClick
        }
    };

    function onClick(e, treeId, treeNode) {
        $("#MenuId").val(treeNode.id);
        //加载数据
        $("#parentName").val(treeNode.name);
    }

    function showMenu() {
        var cityObj = $("#parentName");
        var cityOffset = $("#parentName").offset();
        var editformOffset = $("#editform").offset();
        $("#menuContent").css({ left: cityOffset.left - editformOffset.left + "px", top: (cityOffset.top + cityObj.outerHeight() + 6) - editformOffset.top + "px" }).slideDown("fast");
        $("body").bind("mousedown", onBodyDown);
    }

    function hideMenu() {
        $("#menuContent").fadeOut("fast");
        $("body").unbind("mousedown", onBodyDown);
    }

    function onBodyDown(event) {
        if (!(event.target.id === "menuBtn" || event.target.id === "menuContent" || $(event.target).parents("#menuContent").length > 0)) {
            hideMenu();
        }
    }

    $(document).ready(function () {
        //判断是否具有筛选菜单条件
        var postUrl = "/Common/UserControl/GetOrganizationTreeRemoveChildren";
        UtilAjaxPostAsync(postUrl, {
            organizationId: $("#OrganizationId").val()
        }, function (data) {
            combotreeOrgTreeObj = $.fn.zTree.init($("#orgTree"), setting, data);
        });
        $('#searchOrg').on("click", function (e) {
            search($("#searchOrgKey").val());
        });

        $('#searchOrgKey').on("keyup", function (e) {
            search($(this).val());
        });
    });
    var combotreeOrgTreeObj = null;

    function search(p) {
        if (combotreeOrgTreeObj) {
            var rootNodes = combotreeOrgTreeObj.getNodes(),
                allNodes = combotreeOrgTreeObj.transformToArray(rootNodes),
                checkedNodes = combotreeOrgTreeObj.getCheckedNodes(true);
            $.each(checkedNodes, function () {
                combotreeOrgTreeObj.checkNode(this, false);
            });
            combotreeOrgTreeObj.cancelSelectedNode();
            if (p) {
                combotreeOrgTreeObj.hideNodes(allNodes);
                var nodes = combotreeOrgTreeObj.getNodesByParamFuzzy('name', p),
                    parentNode,
                    o = {};
                $.each(nodes, function (i, node) {
                    if (!node.isParent) {
                        parentNode = node.getParentNode();
                        o[node.tId] = node;
                        while (parentNode) {
                            o[parentNode.tId] || (o[parentNode.tId] = parentNode);
                            parentNode = parentNode.getParentNode();
                        }
                    }
                    return true;
                });
                $.each(o, function (key, value) {
                    combotreeOrgTreeObj.showNode(value);
                });
                $.each(rootNodes, function (i, node) {
                    node.isParent && o[node.tId] && combotreeOrgTreeObj.expandNode(node, true, true);
                });
            } else {
                combotreeOrgTreeObj.showNodes(allNodes);
                combotreeOrgTreeObj.expandAll(false);
            }
        }
    }

</script>